package com.boyi.center.controller.impl;

import com.boyi.center.controller.BaseController;
import com.boyi.shared.common.Page;
import com.boyi.shared.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * RestTemplateControllerImpl.class
 *
 * @author tingwen
 * @date date
 */
@RestController("restfulTest")
public class RestTemplateControllerImpl implements BaseController {
    /**
     * serverId 一定要与 配置文件yml 中的对上
     */
    private final static String SERVER_ID = "microcloud-provider-product";

    private final static String BASE_URL = "http://%s:%s/user/";
    private final static String SAVE = BASE_URL + "save";
    private final static String BATCH_INSERT = BASE_URL + "batchInsert";
    private final static String DELETE = BASE_URL + "delete";
    private final static String UPDATE_NOT_NULL = BASE_URL + "updateNotNull";
    private final static String SELECT_PAGE = BASE_URL + "selectPage";
    private final static String BATCH_DELETE = BASE_URL + "batchDelete";
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpHeaders httpHeaders;
    /**
     * 用来获取服务端的相关信息
     */
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping
    @Override
    public Result<List> selectPage(Object entity, Page page) {
        ServiceInstance serviceInstance = loadBalancerClient.choose(SERVER_ID);
        System.out.println(
                "【*** ServiceInstance ***】host = " + serviceInstance.getHost()
                        + "、port = " + serviceInstance.getPort()
                        + "、serviceId = " + serviceInstance.getServiceId());
        String format = String.format(SELECT_PAGE, serviceInstance.getHost(), serviceInstance.getPort());
        return restTemplate.exchange(format, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PostMapping
    @Override
    public Result<List> save(Object entity) {
        return restTemplate.exchange(SAVE, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @Override
    public Result<List> batchInsert(List entities) {
        return restTemplate.exchange(BATCH_INSERT, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entities).getBody();
    }

    @DeleteMapping
    @Override
    public Result<List> delete(Object entity) {
        return restTemplate.exchange(DELETE, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PutMapping
    @Override
    public Result<List> updateNotNull(Object entity) {
        return restTemplate.exchange(UPDATE_NOT_NULL, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PatchMapping
    @Override
    public Result<List> batchDelete(String[] ids) {
        return restTemplate.exchange(BATCH_DELETE, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, ids).getBody();
    }
}
